package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mqx
 * @date 2020-11-4 11:33:56
 */
@Api(tags = "后台SPU数据接口测试")
@RestController // @ReponseBody + @Controller
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private SpuInfoService spuInfoService;

    // http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    // http://api.gmall.com/admin/product/1/10?category3Id=61
    @GetMapping("{page}/{limit}")
    public Result<IPage<SpuInfo>> getSpuList(@PathVariable Long page,
                                             @PathVariable Long limit,
                                             SpuInfo spuInfo) {

        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);

        IPage<SpuInfo> spuList = spuInfoService.getSpuList(spuInfoPage, spuInfo);

        return Result.ok(spuList);//看到没,我这没输入三级分类,但是我后台拿到了
    }

//    http://localhost/admin/product/spuSaleAttrList/1
//  获取指定spuId的spu销售属性,其中正确填充属性值
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable Long spuId) {

        return Result.ok(spuInfoService.getSpuSaleAttrList(spuId));

    }
    //http://localhost/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result<List<BaseSaleAttr>> getBaseSaleAttrList() {

        return Result.ok(spuInfoService.getBaseSaleAttrList());

    }


    //    http://localhost/admin/product/spuImageList/1
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable Long spuId) {

        return Result.ok(spuInfoService.getSpuImageList(spuId));

    }
//http://localhost/admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {


        spuInfoService.save(spuInfo);

        return Result.ok();

    }




}
